package top.retain;

import java.util.Arrays;

/**
 * @author ivorfan
 * @date 2021/8/1 10:56
 */
public class Solution2 {

    public static void main(String[] args) {
        minimumPerimeter(1000000000);
    }
    public static long numberOfWeeks(int[] milestones) {
        long weeks = 0;
        // 先顺序完成
        int lastWork = -1;
        int i =0;
        long sum = Arrays.stream(milestones).filter(m -> m != 0).count();
        while (sum >= 0) {
            if (i == lastWork && sum == 1) break;
            if (milestones[i] > 0 && i != lastWork) {
                milestones[i]--;
                if (milestones[i] == 0) sum--;
                weeks++;
                lastWork = i;
            }
            // 重新开始
            i++;
            if (i == milestones.length) i = 0;
        }
        return weeks;
    }

    public static long minimumPerimeter(long neededApples) {
        // dp[i] : 边长为i的正方形有多少个苹果
        // 分为4部分，算第一象限
        long lastApples = 0;
        neededApples /= 4;
        int i = 2;
        long currentSum = 0;
        while (currentSum < neededApples) {
            for (int j = 0; j < i; j++) {
                currentSum += 2 * getApples(j, i);
            }
            currentSum += getApples(i, i);
            i++;
        }
        return i * 8;
    }

    public static long getApples(int x, int y) {
        return x + y;
    }
}
